package cn.edu.zut.Business;
import cn.edu.zut.Dao.StudentDAO;
import cn.edu.zut.Model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class Services {
    StudentDAO studentDAO;
    @Autowired
    public void setStudentDAO(StudentDAO studentDAO) {
        this.studentDAO = studentDAO;
    }

    public float avarageScore() {
        List<Student> students = studentDAO.queryAll();
        float scores = 0;
        for(Student s : students) {
            scores += s.getSgrade();
        }

        return scores/students.size();
    }
    public void insertStudent(Student student) {
        studentDAO.insertStudent(student);
    }
    @Transactional
    public void modifysgrade(){
        List<Student>students =studentDAO.queryAll();
        for(Student s: students){
            int sgrade=(int)s.getSgrade();
            int G=sgrade/10;
            switch (G) {
                case 10:
                case 9:s.setSgrade(1f);break;
                case 8:s.setSgrade(2f);break;
                case 7:s.setSgrade(3f);break;
                case 6:s.setSgrade(4f);break;
                default:s.setSgrade(5f);
            }
            System.out.println(s.getSgrade());
            studentDAO.modifyStudent(s);
        }
    }
}
